<template>
  <div class="add-enterprise">
    <header class="add-header">
      <div class="left">
        <span class="arrow" @click="$router.back()"
          ><i class="el-icon-arrow-left" />返回</span
        >
        <span>|</span>
        <span>{{id?'编辑企业':'添加企业'}}</span>
      </div>
      <div class="right">黑马程序员</div>
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">企业信息</div>
        <div class="form">
          <el-form
            ref="ruleForm"
            label-width="100px"
            :model="addForm"
            :rules="rules"
          >
            <el-form-item label="企业名称" prop="name">
              <el-input v-model="addForm.name" />
            </el-form-item>
            <el-form-item label="法人" prop="legalPerson">
              <el-input v-model="addForm.legalPerson" />
            </el-form-item>
            <el-form-item label="注册地址" prop="registeredAddress">
              <el-input v-model="addForm.registeredAddress" />
            </el-form-item>
            <el-form-item label="所在行业" prop="industryCode">
              <el-select v-model="addForm.industryCode">
                <el-option
                  v-for="item in industryCodeList"
                  :key="item.industryCode"
                  :label="item.industryName"
                  :value="item.industryCode"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="企业联系人" prop="contact">
              <el-input v-model="addForm.contact" />
            </el-form-item>
            <el-form-item label="联系电话" prop="contactNumber">
              <el-input v-model="addForm.contactNumber" />
            </el-form-item>
            <el-form-item label="营业执照" prop="businessLicenseUrl">
              <!-- action必传属性 上传文件的路径 不灵活 一般不用 -->
              <!-- 即使不用 也需要配置 如果没配置 需要写一个# -->
              <!-- http-request 自定义上传可以覆盖action -->
              <!-- before-upload上传前执行 -->
              <el-upload
                class="upload-demo"
                action="#"
                :http-request="uploadImage"
                :before-upload="beforeUpload"
              >
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">
                  只能上传jpg/png/jpeg文件,且不超过5M
                </div>
              </el-upload>
              <img v-if="id" :src="addForm.businessLicenseUrl" alt="" :style="{width:'100px'}">
            </el-form-item>
          </el-form>
        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button>重置</el-button>
        <el-button type="primary" @click="confirmAdd">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { getIndustryListAPI,getAddEnterpriseAPI,getEnterpriseDetailAPI,updateExterpriseAPI } from "@/api/enterprise.js";
import { upLoadFile } from "@/api/common.js";
export default {
  data() {
    return {
      addForm: {
        name: "", // 企业名称
        legalPerson: "", // 法人
        registeredAddress: "", // 注册地址
        industryCode: "", // 所在行业
        contact: "", // 企业联系人
        contactNumber: "", // 联系人电话
        businessLicenseUrl: "", // 营业执照url
        businessLicenseId: "", // 营业执照id
      },
      industryCodeList: [],
      rules: {
        name:[{required:true,message:'企业名称不能为空',trigger:'blur'}],
        legalPerson:[{required:true,message:'法人不能为空',trigger:'blur'}],
        registeredAddress:[{required:true,message:'注册地址不能为空',trigger:'blur'}],
        industryCode:[{required:true,message:'所在行业不能为空',trigger:'change'}],
        contact:[{required:true,message:'企业联系人不能为空',trigger:'blur'}],
        contactNumber:[{required:true,message:'联系人电话不能为空',trigger:'blur'},{parent:/^1[3-9]\d{9}$/,message:'电话格式不正确',trigger:'blur'}],
        businessLicenseUrl:[{required:true,message:'营业执照url不能为空',trigger:'blur'}],
      },
    };
  },
  methods:{
    async uploadImage({file}){//获取上传文件
      // console.log(data.file,'data')
      const formData = new FormData()
      //formData追加数据 使用append 
      formData.append('file',file)
      formData.append('type',1)
      const res =  await upLoadFile(formData)
      this.addForm.businessLicenseId = res.data.id
      this.addForm.businessLicenseUrl = res.data.url
      //普通表单可以直接进行校验 上传后校验信息不通过依然显示 所以进行单独校验
      this.$refs.ruleForm.validateField('businessLicenseUrl')
    },
    beforeUpload(file){//上传前校验图片格式和大小
      const imageType = ['image/jpeg','image/jpg','image/png','image/svg+xml']
      if(!imageType.includes(file.type)){
        this.$message.warning('图片类型不正确')
        return false
      }
      if(file.size/1024/1024 > 5){
        this.$message.warning('图片大小不能超过5M')
        return false
      }
    },
    confirmAdd(){
      this.$refs.ruleForm.validate(async(flag)=>{
        if(!flag) return
        if(flag){
         if(this.id){
          delete this.addForm.businessLicenseName
          delete this.addForm.industryName
          delete this.addForm.rent
          const res = await updateExterpriseAPI(this.addForm)
          console.log(res,'updateExterprise')
          this.$message.success('企业编辑成功')
         }else{
          const res = await getAddEnterpriseAPI(this.addForm)
          console.log(res,'getAddEnterpriseAPI')
          this.$message.success('企业添加成功')
         }
          this.$router.back()
        }
      })
    },
    async getEnterpriseDetail(){//查看企业
      const res = await getEnterpriseDetailAPI(this.id)
      console.log(res,'getEnterpriseDetail')
      this.addForm = res.data
    }
  },
  async created() {
    const res = await getIndustryListAPI();
    console.log(res, "getIndustryListAPI");
    this.industryCodeList = res.data;
    if(this.id){
      this.getEnterpriseDetail() // 获取企业详情
    }
    
  },
  computed:{
    id(){//根据id判断是编辑还是添加
      return this.$route.query.id
    }
  }
};
</script>

<style scoped lang="scss">
.add-enterprise {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    height: 64px;
    line-height: 64px;
    font-size: 16px;
    font-weight: bold;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }
      .arrow {
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;
      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }
      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;
        .el-form {
          display: flex;
          flex-wrap: wrap;
          .el-form-item {
            width: 50%;
          }
        }
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
</style>